package gui.visu.dialogs;

import gui.visu.listeners.ListenerCancelButton;
import gui.visu.listeners.ListenerCreateDisplayDataColor;
import gui.visu.listeners.ListenerAddDisplayDataInSpecificPositionOkButton;
import gui.visu.listeners.ListenerLabelTouchInScrollPane;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.ArrayList;
import java.util.Collections;

import javax.swing.BorderFactory;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;

import controls.DataControl;

import tuio.components.ButtonTouch;
import tuio.components.LabelTouch;
import tuio.components.ScrollBarTouch;
import tuio.components.ScrollBarWithoutButtonsUI;
import tuio.components.ScrollPaneTouch;
import tuio.components.SliderTouch;
import data.Constants;



/**
 * @author Charles Perin
 * 29 mars 2011
 * LIMSI - INRIA Rennes
 * charles.perin@free.fr
 *
 */
public class JDialogAddDisplayDataAtPosition extends JDialog {

	private static final long serialVersionUID = -651982042721038870L;

	private DataControl dataControl;
	private SliderTouch positionSlider;
	private String[] displayDatas;
	private Color colorDisplayData;
	private ButtonTouch changeDisplayDataColorButton;
	private ArrayList<LabelTouch> labelList = new ArrayList<LabelTouch>();
	private JLabel selectedLabel;
	private JPanel displayDataPanel = new JPanel(new BorderLayout());

	public JDialogAddDisplayDataAtPosition(DataControl dataControl, String title, boolean modal, String[] displayDatas, int nbpositions) {
		super(dataControl.getIhm(), title, modal);
		setDataControl(dataControl);
		changeDisplayDataColorButton = new ButtonTouch();
		changeDisplayDataColorButton.setPreferredSize(new Dimension(30,30));
		changeDisplayDataColorButton.setBackground(Constants.DISPLAY_DATA_DEFAULT_COLOR);
		setColorDisplayData(Constants.DISPLAY_DATA_DEFAULT_COLOR);
		positionSlider = new SliderTouch(JSlider.VERTICAL, 0, nbpositions, 0);
		this.displayDatas = displayDatas;

		this.setResizable(false);
		this.setUndecorated(true);
		this.initComponent();
		this.pack();
	}

	private void initComponent() {
		JPanel content = new JPanel();
		content.setBackground(Color.white);
		content.setBorder(BorderFactory.createMatteBorder(3, 3, 1, 3, Constants.WINDOW_BORDER_COLOR));
		buildChooseDisplayDataPanel();
		content.add(displayDataPanel);
		content.add(buildDisplayDataOptionsPanel());

		JPanel control = new JPanel();
		control.setBorder(BorderFactory.createMatteBorder(0, 3, 3, 3, Constants.WINDOW_BORDER_COLOR));
		ButtonTouch okButon = new ButtonTouch("OK");

		okButon.addActionListener(new ListenerAddDisplayDataInSpecificPositionOkButton(this));

		ButtonTouch cancelBouton = new ButtonTouch("Cancel");
		cancelBouton.addActionListener(new ListenerCancelButton(this));

		control.add(okButon);
		control.add(cancelBouton);

		this.getContentPane().add(content, BorderLayout.CENTER);
		this.getContentPane().add(control, BorderLayout.SOUTH);
	}

	
	private void buildChooseDisplayDataPanel(){
		
		displayDataPanel.setBackground(Color.WHITE);
		displayDataPanel.setPreferredSize(new Dimension(300, 400));
		displayDataPanel.setBorder(BorderFactory.createTitledBorder("Data to Add"));

		Dimension dimLabel = new Dimension(230, 33);
		
		for(String name:displayDatas){
			labelList.add(new LabelTouch(name));
		}
		Collections.sort(labelList);
		
		JPanel pane = new JPanel(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		c.insets = new Insets(2, 0, 2, 0);
		c.gridx=0;
		c.gridy=0;
		c.weightx=1;
		c.fill=GridBagConstraints.HORIZONTAL;
		
		for(int i=0;i<labelList.size();i++){
			LabelTouch lt = labelList.get(i);
			pane.add(lt,c);
			lt.setPreferredSize(dimLabel);
			lt.setBorder(BorderFactory.createLineBorder(Color.BLACK));
			ListenerLabelTouchInScrollPane labellistener = new ListenerLabelTouchInScrollPane(this, lt);
			lt.addMouseListener(labellistener);
			if(i==0){
				setSelectedLabel(lt);
				lt.setBorder(BorderFactory.createMatteBorder(2, 2, 2, 2, Color.BLACK));
				lt.setForeground(Color.RED);
			}
			c.gridy=c.gridy+1;
		}
		
		ScrollPaneTouch scrollPane = new ScrollPaneTouch(pane);
		scrollPane.setPreferredSize(new Dimension(260,360));
		ScrollBarTouch scrollBarTouch = new ScrollBarTouch();
		scrollBarTouch.setPreferredSize(new Dimension(20,10));
		scrollBarTouch.setUI(new ScrollBarWithoutButtonsUI());
		scrollPane.setVerticalScrollBar(scrollBarTouch);
		
		displayDataPanel.add(scrollPane,BorderLayout.CENTER);
	}
	
	private JPanel buildDisplayDataOptionsPanel(){
		JPanel positionPanel = new JPanel(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		c.insets=new Insets(0, 0, 0, 0);
		c.gridx=0;
		c.gridy=0;
		c.weighty=0;
		c.weightx=1;
		positionPanel.setBackground(Color.WHITE);
		positionPanel.setPreferredSize(new Dimension(150, 400));
		positionPanel.setBorder(BorderFactory.createTitledBorder("New data Position & Color"));
	
		JLabel positionlabel = new JLabel("Position:");
		positionPanel.add(positionlabel,c);
		
		c.gridy=c.gridy+1;
		c.weighty=1;
		c.gridheight=2;
		c.fill=GridBagConstraints.VERTICAL;
		positionSlider.setSnapToTicks(true);
		positionSlider.setPaintLabels(true);
		positionSlider.setPaintTicks(true);
		positionSlider.setBackground(Color.WHITE);
		positionSlider.setMajorTickSpacing(1);
		
		positionPanel.add(positionSlider,c);
		
		c.fill=GridBagConstraints.NONE;
		c.gridheight=1;
		c.gridx=0;
		c.weighty=0;
		c.gridy=0;
		c.gridx=c.gridx+1;
		
		JLabel changeColorLabel = new JLabel("Color:");
		positionPanel.add(changeColorLabel,c);
		
		c.gridy=c.gridy+1;
		c.weighty=1;
		
		changeDisplayDataColorButton.addActionListener(new ListenerCreateDisplayDataColor(dataControl,this));
		positionPanel.add(changeDisplayDataColorButton,c);
		
		return positionPanel;
	}
	


	private void updateColorButton() {
		changeDisplayDataColorButton.setBackground(colorDisplayData);
	}
	
	public SliderTouch getPositionSlider() {
		return positionSlider;
	}

	public void setColorDisplayData(Color colorDisplayData) {
		this.colorDisplayData = colorDisplayData;
		updateColorButton();
	}

	public Color getColorDisplayData() {
		return colorDisplayData;
	}

	public void setDataControl(DataControl dataControl) {
		this.dataControl = dataControl;
	}

	public DataControl getDataControl() {
		return dataControl;
	}

	public String[] getdisplayDatas() {
		return displayDatas;
	}

	public void setSelectedLabel(JLabel selectedLabel) {
		this.selectedLabel = selectedLabel;
	}

	public JLabel getSelectedLabel() {
		return selectedLabel;
	}

}
